import { setGlobal } from '@swda/utils'
//=============================================================================
// Scene_Fu
//=============================================================================
//-----------------------------------------------------------------------------
// Scene_Fu
//
// The scene class of the skill screen.

function Scene_Fu() {
    this.initialize.apply(this, arguments);
}

Scene_Fu.prototype = Object.create(Scene_ItemBase.prototype);
Scene_Fu.prototype.constructor = Scene_Fu;

Scene_Fu.prototype.initialize = function() {
    Scene_ItemBase.prototype.initialize.call(this);
};


Scene_Fu.prototype.create = function() {
    Scene_ItemBase.prototype.create.call(this);
    this.createHelpWindow();
    this.createStatusWindow();
    this.createItemWindow();
    this.createInstructionWindow();
    this.createActorPageWindow();    // 角色翻页
    this._itemWindow.activate();
    //this._itemWindow.selectLast();
};
  

Scene_Fu.prototype.createStatusWindow = function() {
    const [wx, wy, ww, wh] = [-24, -24, 600, 400];
    this._statusWindow = new Window_SkillStatus(wx, wy, ww, wh);
    this._statusWindow.reservePortraitImages();
    this.addChildAt(this._statusWindow._portraitSprite,2);    // 在底层显示立绘
    this.addWindow(this._statusWindow);
  };

  Scene_Fu.prototype.createActorPageWindow = function() {
    this._actorPageWindow = new Window_ActorPage();
    this._actorPageWindow.activate();
    this._actorPageWindow.setScene(this);
    this.addWindow(this._actorPageWindow);
};
Scene_Fu.prototype.start = function() {
    Scene_ItemBase.prototype.start.call(this);
    this.refreshActor();
};

// 使用物品
Scene_Fu.prototype.onItemOk = function() {
    this.actor().setLastMenuFu(this.item());
    this.determineItem();
};

// 获得符咒
Scene_Fu.prototype.determineItem = function() {
    this.useItem();
    this.activateItemWindow();
};
Scene_Fu.prototype.user = function() {
    return this.actor();
};

Scene_Fu.prototype.playSeForItem = function() {
    SoundManager.playUseSkill();
};

//获取一个对应符咒
Scene_Fu.prototype.useItem = function() {
    //this._itemWindow.refresh();
    $gameParty.gainItem($dataItems[this.item().fuItemId], 1);
    this.playSeForItem();
};

Scene_Fu.prototype.onActorChange = function() {
    this.refreshActor();
    this._itemWindow.activate();
    //this._skillTypeWindow.activate();
};

// ================================================================================================================
// 顶部分类菜单
Scene_Fu.prototype.createItemWindow = function() {
    const [x, y, width, height] = [680, 40 + 65, 482, 480];
    //this._itemWindow = new Window_SkillList(x, y, width, height);
    this._itemWindow = new Window_FuList(x, y, width, height);
    this._itemWindow.opacity = 0
    this._itemWindow._scrollbar.setOffset(5)
    this._itemWindow.setHelpWindow(this._helpWindow);
    this._itemWindow.setHandler('ok',     this.onItemOk.bind(this));
    this._itemWindow.setHandler('cancel', this.popScene.bind(this));
    this._itemWindow.setHandler('pagedown', this.nextActor.bind(this));
    this._itemWindow.setHandler('pageup',   this.previousActor.bind(this));
    this.addWindow(this._itemWindow);
    this._itemWindow.activate();
    //this._itemWindow.selectLast();//目前报错
};


Scene_Fu.prototype.refreshActor = function() {
    var actor = this.actor();
    this._statusWindow.setActor(actor);
    this._itemWindow.setActor(actor);
};

// 炼符说明窗口
Scene_Fu.prototype.createInstructionWindow = function() {
    const text = "选择符咒奇术，可将其炼成符咒物品，供任意角色使用。";
    const tw = 26;
    const width = tw * Window_Base.prototype.standardFontSize() + Window_Base.prototype.textPadding() * 2 + Window_Base.prototype.standardPadding() * 2;
    const height = Window_Base.prototype.standardFontSize()  + Window_Base.prototype.textPadding() * 2 + Window_Base.prototype.standardPadding() * 2;
    const [x, y] = [Graphics.boxWidth - width - 16, 16];
    this._instructionWindow = new Window_Instruction(x, y, width, height);
    this._instructionWindow.setText(text);
    this.addWindow(this._instructionWindow);
};


// 详细说明窗口
Scene_Fu.prototype.createHelpWindow = function() {
    this._helpWindow = new Window_Help();
    this._helpWindow.x = 0;
    this._helpWindow.y = Graphics.boxHeight - this._helpWindow.height
    this.addWindow(this._helpWindow);
};

Scene_Fu.prototype.setActorWindowPosition = function () {
    const _window = this._actorWindow
    _window.x = 0.2 * Graphics.boxWidth
    _window.y = 0.4 * Graphics.boxHeight
}

// 记忆符咒指令
Game_Actor.prototype.setLastMenuFu = function(skill) {
    this._lastMenuFu.setObject(skill);
};

Game_Actor.prototype.lastMenuFu = function() {
    return this._lastMenuFu.object();
};

var _Game_Actor_initMembers = Game_Actor.prototype.initMembers;
Game_Actor.prototype.initMembers = function() {
    _Game_Actor_initMembers.call(this);
    this._lastMenuFu = new Game_Item();
};

setGlobal('Scene_Fu', Scene_Fu)